/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.abstractcanvas.awt;

import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;

public class CanvasPath 
{
	private GeneralPath m_curpath = new GeneralPath();

	public GeneralPath getPath()
	{
		return m_curpath;
	}
		
	public void arc(double x, double y, double radius, double startAngle, double endAngle, boolean clockwise)
	{
		Point2D.Double p1 = new Point2D.Double(x+Math.cos(startAngle)*radius, y+Math.sin(startAngle)*radius);
		Point2D.Double p2 = new Point2D.Double(x+Math.cos(endAngle)*radius, y+Math.sin(endAngle)*radius);

		Arc2D.Double arc = new Arc2D.Double(x-radius,y-radius,radius*2, radius*2, 0,0, Arc2D.PIE);
		if (clockwise)
			arc.setAngles(p1,p2);
		else
			arc.setAngles(p2,p1);
		m_curpath.append(arc.getPathIterator(null), false);
	}

	public void beginPath() 
	{
		// Not useful
	}
	
	public void closePath() {
		if (m_curpath.getCurrentPoint() != null)
			m_curpath.closePath();
	}
	
	public void cubicCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y) {
		Point2D p = m_curpath.getCurrentPoint();
		CubicCurve2D.Double c = new CubicCurve2D.Double(p.getX(), p.getY(), cp1x, cp1y,
					cp2x, cp2y, x, y);
		m_curpath.append(c.getPathIterator(null), true);
	}

	public void lineTo(double x, double y) 
	{
		m_curpath.lineTo((float)x, (float)y);
	}
	
	public void moveTo(double x, double y) {
		m_curpath.moveTo((float)x, (float)y);
	}

	public void quadraticCurveTo(double cpx, double cpy, double x, double y) {
		Point2D p = m_curpath.getCurrentPoint();
		QuadCurve2D.Double q = new QuadCurve2D.Double(p.getX(), p.getY(), cpx, cpy, x, y);
		m_curpath.append(q.getPathIterator(null), true);
	}
	
	public void rect(double startX, double startY, double width, double height) {
		this.moveTo(startX, startY);
		this.lineTo(startX+width, startY);
		this.lineTo(startX+width, startY+height);
		this.lineTo(startX, startY+height);
		this.lineTo(startX, startY);
	}
	
}
